package com.ttdc.service;


import java.util.List;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.ttdc.mapper.UserMapper;
import com.ttdc.pojo.Role;
import com.ttdc.pojo.RoleExample;
import com.ttdc.pojo.User;
import com.ttdc.pojo.UserExample;
import com.ttdc.pojo.UserExample.Criteria;
import com.ttdc.service.UserService;  
@Service("UserService") 
public class UserServiceImpl implements UserService{  
    @Autowired  
	private UserMapper userMapper;
    private UserExample example;
    private ApplicationContext applicationContext;
    
    // 判断用户名是否已注册 

	public boolean userRegister(String username) {
		//根据用户名校验数据库中是否有该用户
		UserExample userExample = new UserExample();
		com.ttdc.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andNameEqualTo(username);
		//统计数据库中该用户名的数量，返回一个int,不为空表示该用户名已被注册
		int count = userMapper.countByExample(userExample);
		
		//如果已存在该用户，返回false
		if (count!=0) {
			return false;
		//如果该用户还未注册，返回true
		} else {
			return true;
		}
	}
	  @Override
	    public List<User> selectByAll(Integer roleId) {
	        return userMapper.selectByAll(roleId);
	    }
	
	//角色id
	public int selectByRoleId(Integer roleId){
		RoleExample roleExample=new RoleExample();
		RoleExample.Criteria criteria=roleExample.createCriteria();
		return 0;
		
	}
	
	
	//已经判断该用户名，现注册该用户（注册验证码在前端校验）
	public int userInsert(User user) {
		//由封装的user对象将该新用户插入数据库
		return userMapper.insert(user);
	}
    
	//根据账号和密码判断是否存在该用户
	@Override
	public int selectByNamePassword(String name, String password) {
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		criteria.andPasswordEqualTo(password);
		int num = userMapper.countByExample(example);
		return num;
	}

	// 根据用户名获得该用户名对应的id

	@Override
	public List<User> selectByNameForId(String name) {
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		List<User> user = userMapper.selectByExample(example);
		return user;
	}

	@Override
	public int countByExample(UserExample example) {
		// TODO Auto-generated method stub
		return userMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(UserExample example) {
		// TODO Auto-generated method stub
		return userMapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(User record) {
		// TODO Auto-generated method stub
		return userMapper.insert(record);
	}

	@Override
	public int insertSelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.insertSelective(record);
	}

	@Override
	public List<User> selectByExample(UserExample example) {
		// TODO Auto-generated method stub
		return userMapper.selectByExample(example);
	}

	@Override
	public User selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(User record, UserExample example) {
		// TODO Auto-generated method stub
		return userMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(User record, UserExample example) {
		// TODO Auto-generated method stub
		return userMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(record);
	}
	
	@Override
	public List<User> selectByAll() {
		// TODO Auto-generated method stub
		return userMapper.selectByAll();
	}
	
	
	
}  